package com.lindroy.viewpagerfragmentpractice.fragment

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment

/**
 * @author Lin
 * @date 2022/3/23
 * @function
 */

abstract class BaseFragment(@LayoutRes private val contentLayoutId: Int) : Fragment() {

    private val fragmentName = this::class.java.simpleName

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        showLog("onCreate")
    }

    override fun onCreateView(inflater: LayoutInflater,
                              container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        showLog("onCreateView")
        return inflater.inflate(contentLayoutId,container,false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        showLog("onViewCreated")
    }

    override fun onDestroy() {
        super.onDestroy()
        showLog("onDestroy")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        showLog("onDestroyView")
    }

    private fun showLog(lifecycleName: String) {
        Log.d("Tag", "$lifecycleName - $this")
    }

    fun showMessage(msg: String) {
        Log.d("Tag","$this")
        Toast.makeText(requireContext(), "$fragmentName 收到 $msg", Toast.LENGTH_SHORT).show()
    }

    abstract fun initView()

}